package org.openintents.droidcon.cloud.model;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.Order;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.openintents.droidcon.cloud.PMF;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class Panel {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key id;

	@Persistent
	private Date from;
	@Persistent
	private Date to;

	@Persistent
	@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "roomAlias asc"))
	private List<Slot> slots;

	@NotPersistent
	static DateFormat df = new SimpleDateFormat("HH:mm");

	public Panel() {

	}

	public Panel(String from, String to) {
		setFromTo(from, to);
	}

	public Key getId() {
		return id;
	}

	public Date getFrom() {
		return from;
	}

	public void setFrom(Date from) {
		this.from = from;
	}

	public Date getTo() {
		return to;
	}

	public void setTo(Date to) {
		this.to = to;
	}

	public List<Slot> getSlots() {
		return slots;
	}

	public void setSlots(List<Slot> slots) {
		this.slots = slots;
	}

	public void setFromTo(String from, String to) {
		try {
			this.from = df.parse(from);
			this.to = df.parse(to);
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}

	public String getFromTo() {
		if (from != null && to != null) {
			return df.format(from) + " - " + df.format(to);
		} else {
			return "";
		}
	}

	public String getFromString() {
		return df.format(from);
	}

	public String getToString() {
		return df.format(to);
	}

	public Slot findSlot(long slotId) {
		for (Slot s : getSlots()) {
			if (s.getId().getId() == slotId) {
				return s;
			}
		}
		return null;
	}

}
